function DjangoDemo(){
    this.url="";
    this.params="";
    this.resultBox=$("#resultBox");
    this.typeBox=$("#typeBox");
    this.urlAddressBox=$("#urlAddress");
    this.paramsBox=$("#params");
    this.sendUrl="/home/getdata";
}

DjangoDemo.prototype={

    param2Obj:function(params){
        var str = params;
        var res = str.split("&");
        var obj = {};
        for (var i = 0, len = res.length; i < len; i++) {
            var _temp = res[i].split("=");
            obj[_temp[0]] = _temp[1];
        }
        return obj;
    },

    getCookie: function (name) {
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    },

    reloadBox:function(){
        location.reload();
        return false;
    },

    resetBox:function(){
       this.urlAddressBox.val("");
       this.paramsBox.val("");
       this.resetBox.html("");
    },

    formatJSON:function(data){
       var json = data;
       var html = "";
       try {
            if (json == "") json = "\"\"";
            var obj = eval("[" + json + "]");
            html = ProcessObject(obj[0], 0, false, false, false);
            return "<PRE class='CodeContainer'>"+html+"</PRE>";
        } catch (e) {
            alert("JSON is not well formated:\n" + e.message);
        }
    },

    bindEvent:function(){
        var t=this;
        var sendUrl="/django/home/chgmodinfo/newssdk/1.12/dev/";
        $("#sendData").on("click", function (e) {
            e.preventDefault();

            var data={
                zip:$("#zipBox").val()
            };

            $.ajax({
                type: "post",
                data: data,
                url: sendUrl,
                beforeSend: function (xhr) {
                    var settings = location.href;
                    if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) {
                        xhr.setRequestHeader("X-CSRFToken", t.getCookie('csrftoken'));
                    }
                },
                success: function (res) {
                    console.log(res);
                }
            });
        });
    },

    init:function(){
        this.bindEvent();
    }

};

$(document).ready(function () {
    new DjangoDemo().init();
});